|
1
|
|
|
'use strict'; |
|
2
|
|
|
|
|
3
|
|
|
// Do this as the first thing so that any code reading it knows the right env. |
|
4
|
|
|
process.env.BABEL_ENV = 'development'; |
|
5
|
|
|
process.env.NODE_ENV = 'development'; |
|
6
|
|
|
|
|
7
|
|
|
// Makes the script crash on unhandled rejections instead of silently |
|
8
|
|
|
// ignoring them. In the future, promise rejections that are not handled will |
|
9
|
|
|
// terminate the Node.js process with a non-zero exit code. |
|
10
|
|
|
process.on('unhandledRejection', err => { |
|
11
|
|
|
throw err; |
|
12
|
|
|
}); |
|
13
|
|
|
|
|
14
|
|
|
// Ensure environment variables are read. |
|
15
|
|
|
require('../config/env'); |
|
16
|
|
|
|
|
17
|
|
|
const fs = require('fs'); |
|
18
|
|
|
const chalk = require('chalk'); |
|
19
|
|
|
const webpack = require('webpack'); |
|
20
|
|
|
const WebpackDevServer = require('webpack-dev-server'); |
|
21
|
|
|
const clearConsole = require('react-dev-utils/clearConsole'); |
|
22
|
|
|
const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles'); |
|
23
|
|
|
const { |
|
24
|
|
|
choosePort, |
|
25
|
|
|
createCompiler, |
|
26
|
|
|
prepareProxy, |
|
27
|
|
|
prepareUrls, |
|
28
|
|
|
} = require('react-dev-utils/WebpackDevServerUtils'); |
|
29
|
|
|
const openBrowser = require('react-dev-utils/openBrowser'); |
|
30
|
|
|
const paths = require('../config/paths'); |
|
31
|
|
|
const config = require('../config/webpack.config.dev'); |
|
32
|
|
|
const createDevServerConfig = require('../config/webpackDevServer.config'); |
|
33
|
|
|
|
|
34
|
|
|
const useYarn = fs.existsSync(paths.yarnLockFile); |
|
35
|
|
|
const isInteractive = process.stdout.isTTY; |
|
36
|
|
|
|
|
37
|
|
|
// Warn and crash if required files are missing |
|
38
|
|
|
if (!checkRequiredFiles([paths.appHtml, paths.appIndexJs])) { |
|
39
|
|
|
process.exit(1); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// Tools like Cloud9 rely on this. |
|
43
|
|
|
const DEFAULT_PORT = parseInt(process.env.PORT, 10) || 3000; |
|
44
|
|
|
const HOST = process.env.HOST || '0.0.0.0'; |
|
45
|
|
|
|
|
46
|
|
|
// We attempt to use the default port but if it is busy, we offer the user to |
|
47
|
|
|
// run on a different port. `detect()` Promise resolves to the next free port. |
|
48
|
|
|
choosePort(HOST, DEFAULT_PORT) |
|
49
|
|
|
.then(port => { |
|
50
|
|
|
if (port == null) { |
|
51
|
|
|
// We have not found a port. |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
const protocol = process.env.HTTPS === 'true' ? 'https' : 'http'; |
|
55
|
|
|
const appName = require(paths.appPackageJson).name; |
|
56
|
|
|
const urls = prepareUrls(protocol, HOST, port); |
|
57
|
|
|
// Create a webpack compiler that is configured with custom messages. |
|
58
|
|
|
const compiler = createCompiler(webpack, config, appName, urls, useYarn); |
|
59
|
|
|
// Load proxy config |
|
60
|
|
|
const proxySetting = require(paths.appPackageJson).proxy; |
|
61
|
|
|
const proxyConfig = prepareProxy(proxySetting, paths.appPublic); |
|
62
|
|
|
// Serve webpack assets generated by the compiler over a web sever. |
|
63
|
|
|
const serverConfig = createDevServerConfig( |
|
64
|
|
|
proxyConfig, |
|
65
|
|
|
urls.lanUrlForConfig |
|
66
|
|
|
); |
|
67
|
|
|
const devServer = new WebpackDevServer(compiler, serverConfig); |
|
68
|
|
|
// Launch WebpackDevServer. |
|
69
|
|
|
devServer.listen(port, HOST, err => { |
|
70
|
|
|
if (err) { |
|
71
|
|
|
return console.log(err); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
if (isInteractive) { |
|
74
|
|
|
clearConsole(); |
|
75
|
|
|
} |
|
76
|
|
|
console.log(chalk.cyan('Starting the development server...\n')); |
|
77
|
|
|
openBrowser(urls.localUrlForBrowser); |
|
|
|
|
|
|
78
|
|
|
}); |
|
79
|
|
|
|
|
80
|
|
|
['SIGINT', 'SIGTERM'].forEach(function(sig) { |
|
81
|
|
|
process.on(sig, function() { |
|
82
|
|
|
devServer.close(); |
|
83
|
|
|
process.exit(); |
|
|
|
|
|
|
84
|
|
|
}); |
|
85
|
|
|
}); |
|
86
|
|
|
}) |
|
87
|
|
|
.catch(err => { |
|
88
|
|
|
if (err && err.message) { |
|
89
|
|
|
console.log(err.message); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
process.exit(1); |
|
|
|
|
|
|
92
|
|
|
}); |
|
93
|
|
|
|